import pyautogui
from pynput import keyboard
import time

pyautogui.PAUSE = 0.005
speed = 1.0
quit = False

# 定义一个函数，将pynput的KeyCode转换为pyautogui的key
def pynput_to_pyautogui_key(key):
    key_name = key.strip()
    # 移除不必要的字符，如'Key.'前缀
    if key_name.startswith('Key.'):
        key_name = key_name[4:]
    return key_name

# 执行录制 - 单个操作
def runRecord(parts) : 
    optType = parts[1].strip()

    if optType == "mv":
        x = int(parts[2])
        y = int(parts[3])    #map(int, parts[2:])
        pyautogui.moveTo(x,y,0.01)

    if optType == "mw":
        dx = float(parts[2])
        dy = float(parts[3])
        x = int(parts[4])
        y = int(parts[5])
        # pyautogui.scroll(amount_to_scroll, x=moveToX, y=moveToY)
        pyautogui.vscroll(dy) # 垂直滚动鼠标
        pyautogui.hscroll(dx)  # 水平滚动鼠标

    if optType == "mc":
        keyValue = int(parts[2])
        clkType = int(parts[3])
        x = int(parts[4])
        y = int(parts[5])

        if keyValue == 0 :
            if clkType == 1 :
                pyautogui.mouseDown(x, y, button=pyautogui.MIDDLE)
            else :
                pyautogui.mouseUp(x, y, button=pyautogui.MIDDLE)
        if keyValue == 1 :
            if clkType == 1 :
                pyautogui.mouseDown(x, y, button=pyautogui.LEFT)
            else :
                pyautogui.mouseUp(x, y, button=pyautogui.LEFT)
        if keyValue == 2 :
            if clkType == 1 :
                pyautogui.mouseDown(x, y, button=pyautogui.RIGHT)
            else :
                pyautogui.mouseUp(x, y, button=pyautogui.RIGHT)

    if optType == "kc":
        keyValue = pynput_to_pyautogui_key(parts[2])
        clkType = int(parts[3])
        print(f"------------------- {keyValue}")
        if clkType == 1 :
            if hasattr(keyboard.Key, keyValue):  # 检查是否为特殊键
                key = getattr(keyboard.Key, keyValue)
                keyboard.Controller().press(key)
            else :
                keyboard.Controller().press(eval(keyValue))

        if clkType == 0 :
            if hasattr(keyboard.Key, keyValue):  # 检查是否为特殊键
                key = getattr(keyboard.Key, keyValue)
                keyboard.Controller().release(key)
            else :
                keyboard.Controller().release(eval(keyValue))

# 执行录制 - 组合操作及进度控制
def runRecords(records) : 
    start_time = time.time()

    for record in records:
        parts = record.split(",")
        recordTime = float(parts[0].strip()) * 1/speed

        runTime = time.time() - start_time
        while(recordTime > runTime) : 
            if quit :
                return 
            time.sleep(0.0005)
            runTime = (time.time() - start_time)
        print(record)
        runRecord(parts)

# 执行录制 - 组合操作及进度控制
def runRecordFile(fileName) : 
    print(f"开始执行操作录制:{fileName}")    
    records = []
    with open(fileName, "r") as file:
        for line in file:
            records.append(line.strip())
    runRecords(records)
    print("操作执行完成。")


# runRecordFile("operationRecords.txt")